package cn.cheegon.blog.controller;

import cn.cheegon.blog.common.api.CommonPage;
import cn.cheegon.blog.common.api.CommonResult;
import cn.cheegon.blog.entity.dto.CmsBlogDto;
import cn.cheegon.blog.entity.dto.ProjectDto;
import cn.cheegon.blog.entity.req.ProjectReq;
import cn.cheegon.blog.model.CmsBlog;
import cn.cheegon.blog.model.PmsProject;
import cn.cheegon.blog.service.PmsProjectService;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 项目表 前端控制器
 * </p>
 *
 * @author cheegon
 * @since 2024-06-21
 */
@RestController
@RequestMapping("/admin/project/*")
public class AdminProjectController extends AdminController {
    @Autowired
    private PmsProjectService pmsProjectService;

    @GetMapping("list")
    public CommonResult<CommonPage<ProjectDto>> getList(@RequestParam(value = "kwd", required = false) String kwd,
                                                        @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @RequestParam(name = "pageSize", defaultValue = "20") Integer pageSize) {
        return CommonResult.success(pmsProjectService.getList(kwd, pageNum, pageSize));
    }

    @GetMapping("detail/{id}")
    public CommonResult<ProjectDto> getBlog(@PathVariable Long id) {
        return CommonResult.success(pmsProjectService.getItem(id));
    }

    @PostMapping("add")
    public CommonResult add(@Validated @RequestBody ProjectReq req) {
        checkAdmin();
        pmsProjectService.add(BeanUtil.toBean(req, PmsProject.class));
        return CommonResult.success();
    }

    @PostMapping("edit")
    public CommonResult edit(@Validated @RequestBody ProjectReq req) {
        checkAdmin();
        pmsProjectService.edit(BeanUtil.toBean(req, PmsProject.class));
        return CommonResult.success();
    }


    @GetMapping("del/{id}")
    public CommonResult del(@PathVariable Long id) {
        checkAdmin();
        pmsProjectService.del(id);
        return CommonResult.success();
    }
}
